home *** CD-ROM | disk | FTP | other *** search
/ PC Media 7 / PC MEDIA CD07.iso / share / prog / cm / cmresfil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-06  |  11.2 KB  |  550 lines

  1. // CmResFil.cpp
  2. // -----------------------------------------------------------------
  3. // Compendium - C++ Container Class Library
  4. // Copyright (C) 1992-1994, Glenn M. Poorman, All rights reserved
  5. // -----------------------------------------------------------------
  6. // Reserve binary file interface implementation.
  7. // -----------------------------------------------------------------
  8.  
  9. #include <string.h>
  10. #include <cm/include/cmresfil.h>
  11.  
  12.  
  13. // Define a file buffer size of 16k.
  14. //
  15. #define CM_BUF_SIZE 16384
  16.  
  17.  
  18. // "CmReserveFile" is the default file constructor.
  19. //
  20. CmReserveFile::CmReserveFile()
  21. {
  22.   _name = NULL;
  23.   _file = NULL;
  24. }
  25.  
  26.  
  27. // "CmReserveFile" is the file constructor.  The file is opened/created at
  28. // construct time and the error parameter signals success or failure.
  29. //
  30. CmReserveFile::CmReserveFile(const char* filename)
  31. {
  32.   _file = NULL;
  33.   _name = (strlen(filename) > 0) ? new char[strlen(filename)+1] : NULL;
  34.   if (_name)
  35.   {
  36.     strcpy(_name, filename);
  37.     _file = fopen(_name, "rb+");
  38.     if (!_file) _file = fopen(_name, "wb+");
  39.     if (_file) setvbuf(_file, NULL, _IOFBF, CM_BUF_SIZE);
  40.   }
  41. }
  42.  
  43.  
  44. // "CmReserveFile" is the file copy constructor.  An invalid file
  45. // is created.
  46. //
  47. CmReserveFile::CmReserveFile(const CmReserveFile& F)
  48. {
  49.   _name = (strlen(F._name) > 0) ? new char[strlen(F._name)+1] : NULL;
  50.   strcpy(_name, F._name);
  51.   _file = NULL;
  52. }
  53.  
  54.  
  55. // "~CmReserveFile" is the file destructor.  The file is closed at
  56. // destruct time.
  57. //
  58. CmReserveFile::~CmReserveFile()
  59. {
  60.   delete[] _name;
  61.   if (_file) fclose(_file);
  62. }
  63.  
  64.  
  65. // "=" assignment operator copies the file name from the specified
  66. // file.  However, an invalid file is created.
  67. //
  68. CmReserveFile& CmReserveFile::operator=(const CmReserveFile& F)
  69. {
  70.   if (&F != this)
  71.   {
  72.     if (_file) fclose(_file);
  73.     delete[] _name;
  74.     _name = (strlen(F._name) > 0) ? new char[strlen(F._name)+1] : NULL;
  75.     strcpy(_name, F._name);
  76.     _file = NULL;
  77.   }
  78.   return *this;
  79. }
  80.  
  81.  
  82. // "open" opens the named file.
  83. //
  84. Bool CmReserveFile::open(const char* filename)
  85. {
  86.   if (_file) return FALSE;
  87.  
  88.   if (_name) delete[] _name;
  89.   _name = (strlen(filename) > 0) ? new char[strlen(filename)+1] : NULL;
  90.  
  91.   if (_name)
  92.   {
  93.     strcpy(_name, filename);
  94.     _file = fopen(_name, "rb+");
  95.     if (!_file) _file = fopen(_name, "wb+");
  96.     if (_file) setvbuf(_file, NULL, _IOFBF, CM_BUF_SIZE);
  97.   }
  98.   return (_file) ? TRUE : FALSE;
  99. }
  100.  
  101.  
  102. // "close" closes the file.
  103. //
  104. Bool CmReserveFile::close()
  105. {
  106.   int out = 1;
  107.   if (_file)
  108.   {
  109.     out = fclose(_file);
  110.     _file = NULL;
  111.   }
  112.   return (out == 0) ? TRUE : FALSE;
  113. }
  114.  
  115.  
  116. // "remove" closes and removes this file from the disk.
  117. //
  118. Bool CmReserveFile::remove()
  119. {
  120.   if (!_file) return FALSE;
  121.   close();
  122.   return (::remove(_name) == 0) ? TRUE : FALSE;
  123. }
  124.  
  125.  
  126. // "rename" renames this file.
  127. //
  128. Bool CmReserveFile::rename(const char* newname)
  129. {
  130.   if (::rename(_name, newname) != 0) return FALSE;
  131.   delete[] _name;
  132.   _name = (strlen(newname) > 0) ? new char[strlen(newname)+1] : NULL;
  133.   if (_name) strcpy(_name, newname);
  134.   return (_name) ? TRUE : FALSE;
  135. }
  136.  
  137.  
  138. // "read" is the direct input function for this file.
  139. //
  140. size_t CmReserveFile::read(void* ptr, size_t size, size_t nobj)
  141. {
  142.   return (_file) ? fread(ptr, size, nobj, _file) : 0;
  143. }
  144.  
  145.  
  146. // "read" is a direct input function reading only one object.
  147. //
  148. Bool CmReserveFile::read(void* ptr, size_t size)
  149. {
  150.   return (!_file) ? FALSE : ((fread(ptr, size, 1, _file) == 1) ? TRUE : FALSE);
  151. }
  152.  
  153.  
  154. // "write" is the direct output function for this file.
  155. //
  156. size_t CmReserveFile::write(void* ptr, size_t size, size_t nobj)
  157. {
  158.   return (_file) ? fwrite(ptr, size, nobj, _file) : 0;
  159. }
  160.  
  161.  
  162. // "write" is a direct output function writing only one object.
  163. //
  164. Bool CmReserveFile::write(void* ptr, size_t size)
  165. {
  166.   return (!_file) ? FALSE : ((fwrite(ptr, size, 1, _file) == 1) ? TRUE : FALSE);
  167. }
  168.  
  169.  
  170. // "read" reads one character from the file.
  171. //
  172. Bool CmReserveFile::read(char& c)
  173. {
  174.   return read((void*) &c, sizeof(char));
  175. }
  176.  
  177.  
  178. // "read" reads one short from the file.
  179. //
  180. Bool CmReserveFile::read(short& s)
  181. {
  182.   return read((void*) &s, sizeof(short));
  183. }
  184.  
  185.  
  186. // "read" reads one integer from the file.
  187. //
  188. Bool CmReserveFile::read(int& i)
  189. {
  190.   return read((void*) &i, sizeof(int));
  191. }
  192.  
  193.  
  194. // "read" reads one unsigned character from the file.
  195. //
  196. Bool CmReserveFile::read(long& l)
  197. {
  198.   return read((void*) &l, sizeof(long));
  199. }
  200.  
  201.  
  202. // "read" reads one unsigned character from the file.
  203. //
  204. Bool CmReserveFile::read(unsigned char& uc)
  205. {
  206.   return read((void*) &uc, sizeof(unsigned char));
  207. }
  208.  
  209.  
  210. // "read" reads one unsigned character short the file.
  211. //
  212. Bool CmReserveFile::read(unsigned short& us)
  213. {
  214.   return read((void*) &us, sizeof(unsigned short));
  215. }
  216.  
  217.  
  218. // "read" reads one unsigned int from the file.
  219. //
  220. Bool CmReserveFile::read(unsigned int& ui)
  221. {
  222.   return read((void*) &ui, sizeof(unsigned int));
  223. }
  224.  
  225.  
  226. // "read" reads one unsigned long from the file.
  227. //
  228. Bool CmReserveFile::read(unsigned long& ul)
  229. {
  230.   return read((void*) &ul, sizeof(unsigned long));
  231. }
  232.  
  233.  
  234. // "read" reads one float from the file.
  235. //
  236. Bool CmReserveFile::read(float& f)
  237. {
  238.   return read((void*) &f, sizeof(float));
  239. }
  240.  
  241.  
  242. // "read" reads one double from the file.
  243. //
  244. Bool CmReserveFile::read(double& d)
  245. {
  246.   return read((void*) &d, sizeof(double));
  247. }
  248.  
  249.  
  250. // "read" reads an array of characters from the file.
  251. //
  252. Bool CmReserveFile::read(char* c, unsigned u)
  253. {
  254.   return (read(c, sizeof(char), u) == u);
  255. }
  256.  
  257.  
  258. // "read" reads an array of shorts from the file.
  259. //
  260. Bool CmReserveFile::read(short* s, unsigned u)
  261. {
  262.   return (read(s, sizeof(short), u) == u);
  263. }
  264.  
  265.  
  266. // "read" reads an array of integers from the file.
  267. //
  268. Bool CmReserveFile::read(int* i, unsigned u)
  269. {
  270.   return (read(i, sizeof(int), u) == u);
  271. }
  272.  
  273.  
  274. // "read" reads an array of longs from the file.
  275. //
  276. Bool CmReserveFile::read(long* l, unsigned u)
  277. {
  278.   return (read(l, sizeof(long), u) == u);
  279. }
  280.  
  281.  
  282. // "read" reads an array of floats from the file.
  283. //
  284. Bool CmReserveFile::read(float* f, unsigned u)
  285. {
  286.   return (read(f, sizeof(float), u) == u);
  287. }
  288.  
  289.  
  290. // "read" reads an array of doubles from the file.
  291. //
  292. Bool CmReserveFile::read(double* d, unsigned u)
  293. {
  294.   return (read(d, sizeof(double), u) == u);
  295. }
  296.  
  297.  
  298. // "write" writes one character to the file.
  299. //
  300. Bool CmReserveFile::write(char c)
  301. {
  302.   return write((void*) &c, sizeof(char));
  303. }
  304.  
  305.  
  306. // "write" writes one short to the file.
  307. //
  308. Bool CmReserveFile::write(short s)
  309. {
  310.   return write((void*) &s, sizeof(short));
  311. }
  312.  
  313.  
  314. // "write" writes one integer to the file.
  315. //
  316. Bool CmReserveFile::write(int i)
  317. {
  318.   return write((void*) &i, sizeof(int));
  319. }
  320.  
  321.  
  322. // "write" writes one long to the file.
  323. //
  324. Bool CmReserveFile::write(long l)
  325. {
  326.   return write((void*) &l, sizeof(long));
  327. }
  328.  
  329.  
  330. // "write" writes one unsigned character to the file.
  331. //
  332. Bool CmReserveFile::write(unsigned char uc)
  333. {
  334.   return write((void*) &uc, sizeof(unsigned char));
  335. }
  336.  
  337.  
  338. // "write" writes one unsigned short to the file.
  339. //
  340. Bool CmReserveFile::write(unsigned short us)
  341. {
  342.   return write((void*) &us, sizeof(unsigned short));
  343. }
  344.  
  345.  
  346. // "write" writes one unsigned integer to the file.
  347. //
  348. Bool CmReserveFile::write(unsigned int ui)
  349. {
  350.   return write((void*) &ui, sizeof(unsigned int));
  351. }
  352.  
  353.  
  354. // "write" writes one unsigned long to the file.
  355. //
  356. Bool CmReserveFile::write(unsigned long ul)
  357. {
  358.   return write((void*) &ul, sizeof(unsigned long));
  359. }
  360.  
  361.  
  362. // "write" writes one float to the file.
  363. //
  364. Bool CmReserveFile::write(float f)
  365. {
  366.   return write((void*) &f, sizeof(float));
  367. }
  368.  
  369.  
  370. // "write" writes one double to the file.
  371. //
  372. Bool CmReserveFile::write(double d)
  373. {
  374.   return write((void*) &d, sizeof(double));
  375. }
  376.  
  377.  
  378. // "write" writes an array of characters to the file.
  379. //
  380. Bool CmReserveFile::write(char* c, unsigned u)
  381. {
  382.   return (write(c, sizeof(char), u) == u);
  383. }
  384.  
  385.  
  386. // "write" writes an array of shorts to the file.
  387. //
  388. Bool CmReserveFile::write(short* s, unsigned u)
  389. {
  390.   return (write(s, sizeof(short), u) == u);
  391. }
  392.  
  393.  
  394. // "write" writes an array of integers to the file.
  395. //
  396. Bool CmReserveFile::write(int* i, unsigned u)
  397. {
  398.   return (write(i, sizeof(int), u) == u);
  399. }
  400.  
  401.  
  402. // "write" writes an array of longs to the file.
  403. //
  404. Bool CmReserveFile::write(long* l, unsigned u)
  405. {
  406.   return (write(l, sizeof(long), u) == u);
  407. }
  408.  
  409.  
  410. // "write" writes an array of floats to the file.
  411. //
  412. Bool CmReserveFile::write(float* f, unsigned u)
  413. {
  414.   return (write(f, sizeof(float), u) == u);
  415. }
  416.  
  417.  
  418. // "write" writes an array of doubles to the file.
  419. //
  420. Bool CmReserveFile::write(double* d, unsigned u)
  421. {
  422.   return (write(d, sizeof(double), u) == u);
  423. }
  424.  
  425.  
  426. // "write" writes the specified character string length and the
  427. // string text to the file.
  428. //
  429. Bool CmReserveFile::write(const char* s)
  430. {
  431.   int lnth = (s) ? strlen(s) + 1 : 0;
  432.   if (!write(lnth)) return FALSE;
  433.   return (lnth>0) ? write((char*) s, (unsigned) lnth) : TRUE;
  434. }
  435.  
  436.  
  437. // "read" reads a string length and string text from the file.  The
  438. // output string must be deleted when use is finished.
  439. //
  440. char* CmReserveFile::read()
  441. {
  442.   int lnth;
  443.   if (!read(lnth)) return FALSE;
  444.   char* s = (lnth > 0) ? new char[lnth] : NULL;
  445.   if (s) read(s, (unsigned) lnth);
  446.   return s;
  447. }
  448.  
  449.  
  450. // "seek" sets the file position for this file.
  451. //
  452. int CmReserveFile::seek(long offset, int origin)
  453. {
  454.   return (_file) ? fseek(_file, offset, origin) : -1;
  455. }
  456.  
  457.  
  458. // "tell" returns the current file position for this file.
  459. //
  460. long CmReserveFile::tell()
  461. {
  462.   return (_file) ? ftell(_file) : -1L;
  463. }
  464.  
  465.  
  466. // "rewind" rewinds the file pointer to the beginning of the file.
  467. //
  468. void CmReserveFile::rewind()
  469. {
  470.   if (_file) ::rewind(_file);
  471. }
  472.  
  473.  
  474. // "getPos" records the current file position in ptr.
  475. //
  476. int CmReserveFile::getPos(fpos_t* ptr)
  477. {
  478.   return (_file) ? fgetpos(_file, ptr) : -1;
  479. }
  480.  
  481.  
  482. // "setPos" sets the file position to the position recorded by "getPos"
  483. // in ptr.
  484. //
  485. int CmReserveFile::setPos(const fpos_t* ptr)
  486. {
  487.   return (_file) ? fsetpos(_file, ptr) : -1;
  488. }
  489.  
  490.  
  491. // "clearError" clears all error indicators for this file.
  492. //
  493. void CmReserveFile::clearError()
  494. {
  495.   if (_file) clearerr(_file);
  496. }
  497.  
  498.  
  499. // "eof" checks if the end of file indicator is set for this file.
  500. //
  501. Bool CmReserveFile::eof()
  502. {
  503.   return (!_file) ? -1 : ((feof(_file) != 0) ? TRUE : FALSE);
  504. }
  505.  
  506.  
  507. // "error" checks if any error indicators are set for this file.
  508. //
  509. Bool CmReserveFile::error()
  510. {
  511.   return (!_file) ? -1 : ((ferror(_file) != 0) ? TRUE : FALSE);
  512. }
  513.  
  514.  
  515. // "printError" prints the error message associated with the error
  516. // indicator in the file and also the specified message.
  517. //
  518. void CmReserveFile::printError(const char* s)
  519. {
  520.   perror(s);
  521. }
  522.  
  523.  
  524. // "exists" checks if the specified file exists on the disk.
  525. //
  526. Bool CmReserveFile::exists(const char* filename)
  527. {
  528.   FILE *fp = fopen(filename, "r");
  529.   if (!fp) return FALSE;
  530.   fclose(fp);
  531.   return TRUE;
  532. }
  533.  
  534.  
  535. // "remove" removes the named file from the disk.
  536. //
  537. Bool CmReserveFile::remove(const char* name)
  538. {
  539.   return (::remove(name) == 0) ? TRUE : FALSE;
  540. }
  541.  
  542.  
  543. // "tempName" returns a file name which does not currently exist on the
  544. // disk for use as a temporary file name.
  545. //
  546. char* CmReserveFile::tempName(char* buff)
  547. {
  548.   return tmpnam(buff);
  549. }
  550.